clear all


use "$root\data\use.dta", replace



**# By irrigation
foreach crop in corn soybeans {
	local c_name = strproper("`crop'")

	preserve
	local c = cond("`crop'" == "corn", "c", "s")
	rename `c'_d*_* d*_*
	rename `c'_tbin*_* tbin*_*
	rename `c'_prec_* prec_*
	rename `c'_pdsi_* pdsi_*
	rename `c'_phdi_* phdi_*
	rename `c'_pmdi_* pmdi_*
	
* Planned planting acres, w/wo irrigation
	reghdfe ln`crop'_pplanted d*_b d*_p tbin*_b pdsi_w_b tbin*_p pdsi_w_p if N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 year) vce(cluster fips stateyear)
	estimate store `crop'_pplanted_wo

	reghdfe ln`crop'_pplanted d*_b d*_p tbin*_b pdsi_w_b tbin*_p pdsi_w_p if irrigated_ratio>0.5 , absorb(fips i.state#c.year i.state#c.year2 year) vce(cluster fips stateyear)
	estimate store `crop'_pplanted_w

* Planting ratio, w/wo irrigation
	reghdfe `crop'_plratio d*_b d*_p tbin*_b pdsi_w_b tbin*_p pdsi_w_p if irrigated_ratio==0 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_wo

	reghdfe `crop'_plratio d*_b d*_p tbin*_b pdsi_w_b tbin*_p pdsi_w_p if irrigated_ratio>0.5 , absorb(fips i.state#c.year i.state#c.year2 year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_w

* Harvest ratio

	reghdfe `crop'_hratio d*_b d*_p d*_g tbin*_b pdsi_w_b tbin*_p pdsi_w_p tbin*_g pdsi_w_g if irrigated_ratio==0 & N_year_`crop'>10 & lng>-100 , absorb(fips i.state#c.year i.state#c.year2 year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_wo

	reghdfe `crop'_hratio d*_b d*_p d*_g tbin*_b pdsi_w_b tbin*_p pdsi_w_p tbin*_g pdsi_w_g if irrigated_ratio>0.5, absorb(fips i.state#c.year i.state#c.year2 year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_w
 

*Yield, and Production, w/wo irrigation
foreach y in yield production {
	reghdfe ln`crop'_`y' d*_b d*_p d*_g tbin*_b pdsi_w_b tbin*_p pdsi_w_p tbin*_g pdsi_w_g if irrigated_ratio==0 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 year) vce(cluster fips stateyear)
	estimate store `crop'_`y'_wo

	reghdfe ln`crop'_`y' d*_b d*_p d*_g tbin*_b pdsi_w_b tbin*_p pdsi_w_p tbin*_g pdsi_w_g if irrigated_ratio>0.5 , absorb(fips i.state#c.year i.state#c.year2 year) vce(cluster fips stateyear)
	estimate store `crop'_`y'_w

}
restore
}

**## Plots
coefplot (corn_pplanted_wo, label(No Irrigation) offset(-0.07) ciopts(color(black) )) /// 
	(corn_pplanted_w, label(>50% Irrigated) offset(0.07) ciopts(color(black)) msymbol(S)), bylabel(Corn) || ///
	(soybeans_pplanted_wo, label(No Irrigation) offset(-0.07) ciopts(color(black) )) /// 
	(soybeans_pplanted_w, label(>50% Irrigated) offset(0.07) ciopts(color(black)) msymbol(S)), bylabel(Soybeans) ||, /// 
	mcolor(black) mfcolor(white) vertical keep(d*_b d*_p) yline(0, lcolor(gray) lwidth(0.2)) /// 
	byopts(compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
	groups(d*_b = Pre-planting d*_p = Planting) ytitle("ln(Planned Acres)") ylab(,labs(small)) ///
	xlabel(1 "D0" 2 "D1" 3 "D2" 4 "D3" 6 "D0" 7 "D1" 8 "D2" 9 "D3")
graph export "$root/results_replication/appendix_figures/a3_1_irr_pplanted_re.png", as(png) name("Graph") width(3200) height(2400) replace

coefplot (corn_plratio_wo, label(No Irrigation) offset(-0.07) ciopts(color(black) )) /// 
	(corn_plratio_w, label(>50% Irrigated) offset(0.07) ciopts(color(black)) msymbol(S)), bylabel(Corn) || ///
	(soybeans_plratio_wo, label(No Irrigation) offset(-0.07) ciopts(color(black) )) /// 
	(soybeans_plratio_w, label(>50% Irrigated) offset(0.07) ciopts(color(black)) msymbol(S)), bylabel(Soybeans) ||, /// 
	mcolor(black) mfcolor(white) vertical keep(d*_b d*_p) yline(0, lcolor(gray) lwidth(0.2)) /// 
	byopts(compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
	groups(d*_b = Pre-planting d*_p = Planting) ytitle("Planted Ratio") ylab(,labs(small)) ///
	xlabel(1 "D0" 2 "D1" 3 "D2" 4 "D3" 6 "D0" 7 "D1" 8 "D2" 9 "D3")
graph export "$root/results_replication/appendix_figures/a3_2_irr_plratio_re.png", as(png) name("Graph") width(3200) height(2400) replace


coefplot (corn_hratio_wo, label(No Irrigation) offset(-0.1) ciopts(color(black) )) /// 
		(corn_hratio_w, label(>50% Irrigated) offset(0.1) ciopts(color(black)) msymbol(S)), bylabel(Corn) || ///
		(soybeans_hratio_wo, label(No Irrigation) offset(-0.1) ciopts(color(black) )) /// 
		(soybeans_hratio_w, label(>50% Irrigated) offset(0.1) ciopts(color(black)) msymbol(S)), bylabel(Soybeans) ||, /// 
		mcolor(black) mfcolor(white) vertical keep(d*_b d*_p d*_g) yline(0, lcolor(gray) lwidth(0.2)) /// 
		byopts(compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
		groups(d*_b = Pre-planting d*_p = Planting d*_g = Growing) ytitle("Harvested Ratio") ylab(,labs(small)) ///
		xlabel(1 "D0" 2 "D1" 3 "D2" 4 "D3" 6 "D0" 7 "D1" 8 "D2" 9 "D3" 11 "D0" 12 "D1" 13 "D2" 14 "D3")
graph export "$root/results_replication/appendix_figures/a3_3_irr_hratio_re.png", as(png) name("Graph") width(3200) height(2400) replace

foreach outcome in yield production {
    local y_name = strproper("`outcome'")
	local order = cond("`outcome'" == "hratio", 3, cond("`outcome'" == "yield", 4, 5))
	coefplot (corn_`outcome'_wo, label(No Irrigation) offset(-0.1) ciopts(color(black) )) /// 
		(corn_`outcome'_w, label(>50% Irrigated) offset(0.1) ciopts(color(black)) msymbol(S)), bylabel(Corn) || ///
		(soybeans_`outcome'_wo, label(No Irrigation) offset(-0.1) ciopts(color(black) )) /// 
		(soybeans_`outcome'_w, label(>50% Irrigated) offset(0.1) ciopts(color(black)) msymbol(S)), bylabel(Soybeans) ||, /// 
		mcolor(black) mfcolor(white) vertical keep(d*_b d*_p d*_g) yline(0, lcolor(gray) lwidth(0.2)) /// 
		byopts(compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
		groups(d*_b = Pre-planting d*_p = Planting d*_g = Growing) ytitle("ln(`y_name')") ylab(,labs(small)) ///
		xlabel(1 "D0" 2 "D1" 3 "D2" 4 "D3" 6 "D0" 7 "D1" 8 "D2" 9 "D3" 11 "D0" 12 "D1" 13 "D2" 14 "D3")
	graph export "$root/results_replication/appendix_figures/a3_`order'_irr_`outcome'_re.png", as(png) name("Graph") width(3200) height(2400) replace
}


**# By yield
foreach crop in corn soybeans {
	local c_name = strproper("`crop'")

	preserve
	local c = cond("`crop'" == "corn", "c", "s")
	rename `c'_d*_* d*_*
	rename `c'_tbin*_* tbin*_*
	rename `c'_prec_* prec_*
	rename `c'_pdsi_* pdsi_*
	rename `c'_phdi_* phdi_*
	rename `c'_pmdi_* pmdi_*

	gen yg = .
	forvalues y = 2/22 {
		qui summarize `crop'_yield00 if year == `y', detail 
		replace yg = cond(`crop'_yield00 >= r(p50), 1, 0) if year==`y'
	}

* Planned planting acres
	reghdfe ln`crop'_pplanted d*_b pdsi_w_b tbin*b d*_p pdsi_w_p tbin*p if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100&yg==0, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_pplant_lo
	
	reghdfe ln`crop'_pplanted d*_b pdsi_w_b tbin*b d*_p pdsi_w_p tbin*p if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100&yg==1, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_pplant_hi

* Planting ratio
	reghdfe `crop'_plratio d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100&yg==0, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_lo
	
	reghdfe `crop'_plratio d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100&yg==1, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_hi

* Harvest ratio

	reghdfe `crop'_hratio d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100&yg==0, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_lo
	
	reghdfe `crop'_hratio d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100&yg==1, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_hi

* yield, and production
foreach y in yield production {
	reghdfe ln`crop'_`y' d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100&yg==0, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_`y'_lo
	
	reghdfe ln`crop'_`y' d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100&yg==1, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_`y'_hi
}	

restore
}

**## Plots
coefplot (corn_pplant_lo, label(Low yield) offset(-0.07) ciopts(color(black) )) /// 
	(corn_pplant_hi, label(High yield) offset(0.07) ciopts(color(black)) msymbol(S)), bylabel(Corn) || ///
	(soybeans_pplant_lo, label(Low yield) offset(-0.07) ciopts(color(black)  )) /// 
	(soybeans_pplant_hi, label(High yield) offset(0.07) ciopts(color(black)) msymbol(S)), bylabel(Soybeans) ||, ///
	mcolor(black) mfcolor(white) vertical keep(d*_b d*_p) yline(0, lcolor(gray) lwidth(0.2)) /// 
	byopts(compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
	groups(d*_b = Pre-planting d*_p = Planting) ytitle("ln(Planned Acres)") ylab(,labs(small)) /// 
	xlabel(1 "D0" 2 "D1" 3 "D2" 4 "D3" 6 "D0" 7 "D1" 8 "D2" 9 "D3")
graph export "$root/results_replication/appendix_figures/a4_1_yield_pplant.png", as(png) name("Graph") width(3200) height(2400) replace

coefplot (corn_plratio_lo, label(Low yield) offset(-0.07) ciopts(color(black) )) /// 
	(corn_plratio_hi, label(High yield) offset(0.07) ciopts(color(black)) msymbol(S)), bylabel(Corn) || ///
	(soybeans_plratio_lo, label(Low yield) offset(-0.07) ciopts(color(black)  )) /// 
	(soybeans_plratio_hi, label(High yield) offset(0.07) ciopts(color(black)) msymbol(S)), bylabel(Soybeans) ||, ///
	mcolor(black) mfcolor(white) vertical keep(d*_b d*_p) yline(0, lcolor(gray) lwidth(0.2)) /// 
	byopts(compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
	groups(d*_b = Pre-planting d*_p = Planting) ytitle("Planted Ratio") ylab(,labs(small)) /// 
	xlabel(1 "D0" 2 "D1" 3 "D2" 4 "D3" 6 "D0" 7 "D1" 8 "D2" 9 "D3")
graph export "$root/results_replication/appendix_figures/a4_2_yield_plratio.png", as(png) name("Graph") width(3200) height(2400) replace

coefplot (corn_hratio_lo, label(Low yield) offset(-0.1) ciopts(color(black) )) /// 
	(corn_hratio_hi, label(High yield) offset(0.1) ciopts(color(black)) msymbol(S)), bylabel(Corn) || ///
	(soybeans_hratio_lo, label(Low yield) offset(-0.1) ciopts(color(black)  )) /// 
	(soybeans_hratio_hi, label(High yield) offset(0.1) ciopts(color(black)) msymbol(S)), bylabel(Soybeans) ||, ///
	mcolor(black) mfcolor(white) vertical keep(d*_b d*_p d*_g) yline(0, lcolor(gray) lwidth(0.2)) /// 
	byopts(compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
	groups(d*_b = Pre-planting d*_p = Planting d*_g = Growing) ytitle("Harvested Ratio") ylab(,labs(small)) /// 
	xlabel(1 "D0" 2 "D1" 3 "D2" 4 "D3" 6 "D0" 7 "D1" 8 "D2" 9 "D3" 11 "D0" 12 "D1" 13 "D2" 14 "D3")
graph export "$root/results_replication/appendix_figures/a4_3_yield_hratio.png", as(png) name("Graph") width(3200) height(2400) replace

foreach outcome in yield production {
	local y_name = strproper("`outcome'")
	local order = cond("`outcome'" == "hratio", 3, cond("`outcome'" == "yield", 4, 5))
	coefplot (corn_`outcome'_lo, label(Low yield) offset(-0.1) ciopts(color(black) )) /// 
	(corn_`outcome'_hi, label(High yield) offset(0.1) ciopts(color(black)) msymbol(S)), bylabel(Corn) || ///
	(soybeans_`outcome'_lo, label(Low yield) offset(-0.1) ciopts(color(black)  )) /// 
	(soybeans_`outcome'_hi, label(High yield) offset(0.1) ciopts(color(black)) msymbol(S)), bylabel(Soybeans) ||, ///
	mcolor(black) mfcolor(white) vertical keep(d*_b d*_p d*_g) yline(0, lcolor(gray) lwidth(0.2)) /// 
	byopts(compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
	groups(d*_b = Pre-planting d*_p = Planting d*_g = Growing) ytitle("ln(`y_name')") ylab(,labs(small)) /// 
	xlabel(1 "D0" 2 "D1" 3 "D2" 4 "D3" 6 "D0" 7 "D1" 8 "D2" 9 "D3" 11 "D0" 12 "D1" 13 "D2" 14 "D3")
	graph export "$root/results_replication/appendix_figures/a4_`order'_yield_`outcome'.png", as(png) name("Graph") width(3200) height(2400) replace
}

